import { ref } from "vue";
//成员类接口
interface IRegisterUser {

  name: string;
  email: null;
  password: string;
  password2: string;
  rule: string;

}
//成员
export const registerUser = ref<IRegisterUser>({
  name: "",
  email: null,
  password: "",
  password2: "",
  rule: "",
});
//重复密码校验
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== registerUser.value.password) {
    callback(new Error("两次输入密码不一致!"));
  } else {
    callback();
  }
};
//校验规则接口
interface IRegisterRules {
  name: ({
    required: boolean;
    message: string;
    trigger: string;

  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;

  })[];
  email: ({
    required: boolean;
    validator: string;
    message: string;
    trigger: string;

  } | {
    type: string;
    message: string;
    trigger: string[];

  })[];
  password: ({
    required: boolean;
    message: string;
    trigger: string;

  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;

  })[];

  password2: ({
    required: boolean;
    message: string;
    trigger: string;

  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;

  } | {
    validator: (rule: IRegisterRules, value: string, callback: any) => void;
    trigger: string;
  })[];
}
//校验规则
export const registerRules = ref<IRegisterRules>({
  email: [
    {
      required: true,
      validator: "email",
      message: "邮箱不能为空哦",
      trigger: "blur",
    },
    { type: "email", message: "请输入正确的邮箱", trigger: ['blur', 'change'] },
  ],
  name: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 2, max: 15, message: "用户名需要在2-15个字符", trigger: "blur" },

  ],

  password: [
    { required: true, message: "密码不能为空哦~", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度需要在在6-20位", trigger: "blur" },
  ],
  password2: [
    { required: true, message: "确认密码不能为空哦~", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度需要在在6-20位", trigger: "blur" },
    { validator: validatePass2, trigger: "blur" },
  ],
});